package net.lab1024.sa.admin.module.crawler.service;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import net.lab1024.sa.admin.module.crawler.dao.ShopCopyHisDao;
import net.lab1024.sa.admin.module.crawler.domain.entity.ShopCopyHisEntity;
import net.lab1024.sa.admin.module.crawler.domain.form.ShopCopyHisQueryForm;
import net.lab1024.sa.admin.module.crawler.domain.form.ShopCopyTaskForm;
import net.lab1024.sa.admin.module.crawler.domain.vo.ShopCopyHisVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.module.support.minio.MinioService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 复制记录 Service
 *
 * @Author w
 * @Date 2025-03-29 06:03:19
 * @Copyright w
 */

@Service
public class ShopCopyHisService {

    @Resource
    private ShopCopyHisDao shopCopyHisDao;
    @Autowired
    MinioService minioService;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<ShopCopyHisVO> queryPage(ShopCopyHisQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ShopCopyHisVO> list = shopCopyHisDao.queryPage(page, queryForm);
        PageResult<ShopCopyHisVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    public ShopCopyHisVO getOneCopyHis(ShopCopyHisQueryForm form) {
        ShopCopyHisEntity his = shopCopyHisDao.selectOne(new QueryWrapper<ShopCopyHisEntity>()
                .eq("sshop_id", form.getSshopId())
                .eq("tshop_id", form.getTshopId())
                        .orderByDesc("create_time")
                .last("limit 1")
        );
        if (his != null) {
            ShopCopyHisVO copyHis = SmartBeanUtil.copy(his, ShopCopyHisVO.class);
            String shopDataPath = his.getShopDataPath();
            if (form.getIsRequireData() != null && form.getIsRequireData() && StringUtils.isNotBlank(shopDataPath)) {
                String shopData = minioService.readData(shopDataPath);
                ShopCopyTaskForm copyTaskForm = JSONObject.parseObject(shopData, ShopCopyTaskForm.class);
                copyHis.setDataForm(copyTaskForm);
            }
            return copyHis;
        } else {
            return null;
        }
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        shopCopyHisDao.batchUpdateDeleted(idList, true);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(String id) {
        if (null == id) {
            return ResponseDTO.ok();
        }

        shopCopyHisDao.updateDeleted(id, true);
        return ResponseDTO.ok();
    }
}
